package cn.backpackerxl.service.impl;

import cn.backpackerxl.dao.StudentDao;
import cn.backpackerxl.easyJDBC.annotation.Query;
import cn.backpackerxl.easyJDBC.main.EasyJDBCRepository;
import cn.backpackerxl.entity.Source;
import cn.backpackerxl.entity.Student;
import cn.backpackerxl.pojo.StudentSource;
import cn.backpackerxl.service.SourceService;
import cn.backpackerxl.service.StudentService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.backpackerxl.easyJDBC.util.MySQL.*;
import static cn.backpackerxl.easyJDBC.util.SqlFactory.compileSQL;

/**
 * @author: backpackerxl
 * @create: 2021/12/29
 * @filename: StudentServiceImpl
 **/
public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao = (StudentDao) new EasyJDBCRepository(StudentDao.class).init();
    private final SourceService sourceService = new SourceServiceImpl();

    @Override
    public List<Student> findByName(String name) {
        List<Source> sourceList = sourceService.findByName(name);
        Map<String, Source> sourceMap = new HashMap<>();
        sourceList.forEach(s -> sourceMap.put(s.getName(), s));
        List<Student> students = studentDao.findByOne(name);
        students.forEach(stu -> {
            if (sourceMap.containsKey(stu.getName())) {
                stu.setSource(sourceMap.get(stu.getName()));
            }
        });
        return students;
    }

    @Override
    @Query("select t1.name,t1.sex,t1.age,t2.hadoop,t2.hive,t2.java_web,t1.hobey,t1.create_time from t_student t1 left join t_source t2 on t1.name = t2.name")
    public List<StudentSource> findStudentLeftSource() {
        return studentDao.findByJoin();
    }

    @Override
    @Query("select t1.name,t1.sex,t1.age,t2.hadoop,t2.hive,t2.java_web,t1.hobey,t1.create_time from t_student t1 left join t_source t2 on t1.name = t2.name where t1.name=?")
    public List<StudentSource> findStudentLeftSourceByName(Object... name) {
        return studentDao.findByJoin(name);
    }

    @Override
    @Query("select * from t_student where name=? and sex=?")
    public List<Student> findByQuery(Object... obj) {
        return studentDao.findByQuery(obj);
    }

    @Override
    public List<Student> findByQueries(Object... obj) {
        return studentDao.findByQuery((stu) -> compileSQL(SELECT, $, FROM, stu.getTableName(), WHERE, stu.getFNI(1).suffix("=?"), AND, stu.getFNI(2).suffix("=?")), obj);
    }

    @Override
    public List<StudentSource> findByLeftJoin(Object... name) {
        return studentDao.findByJoin(
                Source.class,
                (stu, sou) -> compileSQL(
                        SELECT,
                        stu.getFNI("t1.", ",", 1, 2, 3, 4, 5),
                        sou.getFNI("t2.", ",", 2, 3, 4),
                        FROM, stu.getTableName("t1"), LEFT, JOIN,
                        sou.getTableName("t2"), ON,
                        stu.getFNI(1).prefix("t1."), eq,
                        sou.getFNI(1).prefix("t2."), WHERE,
                        stu.getFNI(1).prefix("t1.").suffix("=?")),
                name);
    }

    @Override
    public List<StudentSource> findByLeft() {
        return studentDao.findByJoin(
                Source.class,
                (stu, sou) -> compileSQL(
                        SELECT,
                        stu.getFNI("t1.", ",", 1, 2, 3, 4, 5),
                        sou.getFNI("t2.", ",", 2, 3, 4),
                        FROM,
                        stu.getTableName("t1"),
                        LEFT, JOIN,
                        sou.getTableName("t2"),
                        ON,
                        stu.getFNI(1).prefix("t1."),
                        eq,
                        sou.getFNI(1).prefix("t2.")
                ));
    }
}
